// subPages/mine/feedback/feedback.js
const app = getApp();
var util =  require('../../../utils/util.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'feedback'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/business/down.png',
    upImg: '/resources/business/up.png',
    statusArray: ["未处理意见", "已处理意见"],
    statusIndex: 0,
    status: 0,
    date: '',
    isDate: false,
    mainType: 1,
    evaluationId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      statusImgSrc: that.data.downImg
    })
    loadMoreView = that.selectComponent("#loadMoreView")
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getData()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getData()
        }
      }
    }
  },

  //状态选择
  bindStatusChange:function(e){
    // console.log(e)
    var that = this
    that.setData({
      statusIndex: e.detail.value,
      status: e.detail.value,
      statusImgSrc: that.data.downImg,
    })
    that.getData()
  },

  bindStatusCancel:function(){
    this.setData({
      statusImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    if(that.data.statusImgSrc == that.data.downImg){
      that.setData({
        statusImgSrc: that.data.upImg
      })
    }
  },

  //评论状态按钮
  typeBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type;
    that.setData({
      mainType: type
    })
    that.getData()
  },

  //获取评论数据
  getData:function(){
    var that = this
    var start_time = '',end_time = ''
    if(that.data.date){
      start_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 00:00:00').getTime())/1000
      end_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 23:59:59').getTime())/1000
    }
    util.getAjax(`${app.globalData.apiUrl}/community_get_shop_tell`,that.getFeedBackData,{p:p,num:num,user_id: that.data.userId,status: that.data.status,start_time: start_time,end_time: end_time,type: that.data.mainType,type2: 2},that.getFeedBackErr)
  },

  getFeedBackData(res){
    var that = this
    console.log(res)
    if(res.data.list.length != 0){
      for(var i in res.data.list){
        if(res.data.list[i].status){
          res.data.list[i].statusTxt = res.data.list[i].status == 0?'等待处理...':'已处理'
        }
      }
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.evaluationList.concat(res.data.list)
      }
      that.setData({
        evaluationList: list
      })
    }else{
      that.setData({
        evaluationList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  getFeedBackErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取评论失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getData()
  },

  clickLoadMore: function(e) {
    this.getData()
  },

  //回到顶部
  goTop:function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.userId){
      that.getData()
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getData()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})